<script setup lang="ts">
import { onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import 'echarts-liquidfill'
import moment from 'moment'

const chartsRef = ref<any>()

// 获取过去30天的日期
const getPast30Dates = () => {
  const past30Dates = []
  for (let i = 30; i > 0; i--) {
    past30Dates.push(moment().subtract(i, 'd').format('MM/DD'))
  }
  return past30Dates
}

const trendData = {
  dates: getPast30Dates(),
  nums: [
    108291, 112847, 110469, 107420, 89776, 93711, 106511, 114491, 115925, 99992,
    119598, 107278, 101254, 114054, 121996, 127312, 118654, 121950, 111043,
    112367, 113929, 113812, 110930, 121665, 124137, 103273, 102050, 115473,
    118467, 120781,
  ],
}
const initCharts = () => {
  const myCharts = echarts.init(chartsRef.value)

  const options = {
    title: {
      text: '游客趋势',
      textStyle: {
        color: '#fff',
      },
      right: '1%',
      top: '0',
    },
    grid: {
      left: '1%',
      right: '1%',
      bottom: '1%',
      top: '16%',
      containLabel: true,
    },
    textStyle: {
      color: '#fff',
    },
    legend: {
      show: false,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: trendData.dates,
    },
    yAxis: {
      type: 'value',
      min: 'dataMin',
      axisLine: {
        show: true,
      },
      splitLine: {
        show: false,
      },
    },
    tooltip: {
      trigger: 'axis',
    },
    series: [
      {
        name: '游客数量',
        type: 'line',
        data: trendData.nums,
        smooth: true,
        itemStyle: {
          borderWidth: 0,
          opacity: 0,
        },
        lineStyle: {
          width: 1,
          color: '#fff100',
        },
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: '#fff100',
            },
            {
              offset: 1,
              color: 'rgba(199,189,8,0)',
            },
          ]),
        },
      },
    ],
  }
  myCharts.setOption(options)
}
onMounted(() => {
  initCharts()
})
</script>

<template>
  <div class="data">
    <div class="top">
      <p class="title">过去30天游客趋势</p>
    </div>
    <div class="charts" ref="chartsRef"></div>
  </div>
</template>

<style scoped lang="scss">
$title-height: 32px;

.data {
  flex: 1;
  margin: 0 20px;
  background: url('@/assets/images/data_screen/dataScreen-main-cb.png')
    no-repeat;
  background-size: 100% 100%;

  .top {
    font-size: 20px;
    line-height: $title-height;
    color: #fff;

    .title {
      &::after {
        top: $title-height;
        left: 0;
        display: block;
        width: 68px;
        height: 7px;
        content: '';
        background: url('@/assets/images/data_screen/figure.png') no-repeat;
        background-size: cover;
      }
    }
  }

  .charts {
    align-items: center;
    width: 100%;
    height: 250px;
  }
}
</style>
